package com.example.demo.controller;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.TaskDTO;
import com.example.demo.dto.VersionStatisticsDTO;
import com.example.demo.dto.DepStatisticsDTO;
import com.example.demo.dto.AppStatisticsDTO;
import com.example.demo.entity.Task;
import com.example.demo.request.CreateTaskRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/api/tasks")

public class TaskController {

    @Autowired
    private TaskService taskService;


    @GetMapping
    public ResponseEntity<ApiResponse<java.util.List<TaskDTO>>> getTasks(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
             @RequestParam(required = false) String status,
             @RequestParam(required = false) String application,
             @RequestParam(required = false) String department,
            HttpServletRequest request) {

        // 查询数据
        java.util.List<TaskDTO> tasks = taskService.getTasks(page, size, status, application, department);
        long total = taskService.countTasks(status, application, department);
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(tasks, "请求成功", request.getRequestURI(), extra)
        );
    }


    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<TaskDTO>> getTask(
             @PathVariable Long id,
            HttpServletRequest request) {

        TaskDTO task = taskService.getTaskById(id);
        return ResponseEntity.ok(
                ApiResponse.success(task, "查询成功", request.getRequestURI(), null)
        );
    }


    @PostMapping
    public ResponseEntity<ApiResponse<TaskDTO>> createTask(
            @Valid @RequestBody CreateTaskRequest request,
            HttpServletRequest requestObj) {

        Task task = taskService.createTask(request);
        TaskDTO dto = TaskDTO.fromEntity(task, request.getCreatedBy());
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "任务创建成功", requestObj.getRequestURI())
        );
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<TaskDTO>> updateTask(
            @PathVariable Long id,
            @Valid @RequestBody CreateTaskRequest request,
            HttpServletRequest requestObj) {

        taskService.updateTask(id, request);
        TaskDTO task = taskService.getTaskById(id);
        return ResponseEntity.ok(
                ApiResponse.success(task, "任务更新成功", requestObj.getRequestURI(), null)
        );
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteTask(
            @PathVariable Long id,
            HttpServletRequest requestObj) {

        taskService.deleteTask(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "任务删除成功", requestObj.getRequestURI(), null)
        );
    }
    

    @GetMapping("/statistics/version")
    public ResponseEntity<ApiResponse<List<VersionStatisticsDTO>>> getVersionStatistics(
            HttpServletRequest request) {
        
        List<VersionStatisticsDTO> statistics = taskService.getVersionStatistics();
        return ResponseEntity.ok(
                ApiResponse.success(statistics, "统计成功", request.getRequestURI(), null)
        );
    }
    

    @GetMapping("/statistics/dep")
    public ResponseEntity<ApiResponse<List<DepStatisticsDTO>>> getDepStatistics(
             @RequestParam(required = false) String version,
            HttpServletRequest request) {
        
        List<DepStatisticsDTO> statistics = taskService.getDepStatistics(version);
        return ResponseEntity.ok(
                ApiResponse.success(statistics, "统计成功", request.getRequestURI(), null)
        );
    }
    

    @GetMapping("/statistics/app")
    public ResponseEntity<ApiResponse<List<AppStatisticsDTO>>> getAppStatistics(
             @RequestParam(required = false) String version,
             @RequestParam(required = false) String department,
            HttpServletRequest request) {
        
        List<AppStatisticsDTO> statistics = taskService.getAppStatistics(version, department);
        return ResponseEntity.ok(
                ApiResponse.success(statistics, "统计成功", request.getRequestURI(), null)
        );
    }
}